package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IHospitalService;
import com.naiterui.ehp.bs.patient.service.IParametersService;
import com.naiterui.ehp.bs.patient.utils.cache.BaseParamCacheUtil;
import com.naiterui.ehp.bs.patient.vo.CacheDataVO;
import com.naiterui.ehp.bs.patient.vo.common.HospitalVO;
import com.naiterui.ehp.bs.patient.vo.parameters.ParametersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统常用参数业务控制器
 *
 * @version 2015年6月24日 上午10:44:58 by Amber
 * @Description 提供系统一下参数类信息的获取
 */
@Api(tags = SwaggerTag.PATIENT + SwaggerTag.BASIC_INFO)
@RequestMapping("ap/patient")
@Controller
public class ApParametersController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApParametersController.class);

    @Autowired
    private IParametersService parametersService;

    @Autowired
    private IHospitalService hospitalService;

    @ApiOperation(value = "刷新参数配置缓存")
    @ResponseBody
    @RequestMapping("refresh")
    public ResponseVO<Void> refreshParams() throws BusinessException {
        parametersService.refreshParameters();
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @ResponseBody
    @RequestMapping("params")
    @ApiOperation(value = "获取参数配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ParametersVO getParameters() throws BusinessException {
        return parametersService.getParameters();
    }

    @ResponseBody
    @RequestMapping("cacheData")
    @ApiOperation(value = "获取城市及科室列表缓存", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public CacheDataVO getCacheData(Long cityVers, Long depVers) throws BusinessException {
        return parametersService.getCacheData(cityVers, depVers);
    }

    @ResponseBody
    @RequestMapping("nextCity")
    @ApiOperation(value = "获取城市缓存新版本号", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public void getNextVersionCity() {
        BaseParamCacheUtil.getCacheCityNextVersion();
    }

    /**
     * 按城市或名字获取医院列表 ParametersController.getHospitals()<BR>
     */
    @ResponseBody
    @RequestMapping("hospitals")
    @ApiOperation(value = "查询医院列表", notes = "按城市ID搜索医院", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<HospitalVO> getHospitals(Integer cityId, String name) {
        LOGGER.info("hospitals SUCCESS ; cityId {},name {}", cityId, name);
        return hospitalService.getHospitalList(cityId, name);

    }

    /**
     * <搜索医院>
     * <详细描述>
     *
     * @param keyword 关键字
     */
    @ResponseBody
    @RequestMapping("hospital/search")
    @ApiOperation(value = "根据关键词搜索医院名", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public String searchHospitals(String keyword) throws BusinessException {
        // 查询
        String result = hospitalService.searchHospitals(keyword);
        LOGGER.info("search hospital success keyword is {}", keyword);
        return result;
    }

    /**
     * 定位城市+关键字搜索
     *
     * @param keyword 关键字
     */
    @ResponseBody
    @RequestMapping("hospital/search/lbs")
    @ApiOperation(value = "根据定位城市和关键字搜索医院", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<HospitalVO> searchHospitalsByCity(String keyword, String lat, String lng, HttpServletRequest request) throws BusinessException {
        LOGGER.info("根据定位城市和关键字搜索医院：keyword={}", keyword);
        // 查询
        List<HospitalVO> result = hospitalService.searchByLocation(keyword, lat, lng);

//        List<HospitalVO> result = hospitalService.searchByCity(keyword, request);

        return result;
    }
}
